import DeviceManager from './index';
import { isFunction } from 'lodash-es';

/**
 * TODO:1.原型链模式实现：
 * @param instance
 */
export const prototype = (instance: any) => {
    /**
     * 原型链
     */
    instance.__proto__.__proto__ = DeviceManager;

    Object.defineProperties(instance, {
        index: {
            get() {
                // @ts-ignore
                return this.deviceInstance.findIndex((device) => device.instance === this);
            },
        },
        to: {
            get() {
                if (window?.JeeWeb?.OS !== 0x0b) {
                    return this._to;
                }

                // @ts-ignore
                return global?.appInfo?.deviceBind?.[this.index]?.devices || global?.appInfo?.deviceList?.[this.index];
            },
        },
    });

    /**
     * 属性
     */
    instance.callbacks = [];

    instance.timerCallbacks = [];

    instance.onlineCallbacks = [];

    instance.batteryCallbacks = [];

    instance.networkCallbacks = [];

    /**
     * 订阅
     */
    instance.onReceive = function (...rest: any) {
        this.warn('方法 onReceive 已废弃，请使用 subscribe 代替');

        this.subscribe(...rest);
    };

    instance.subscribe = function (key: string[], callback: any) {
        if (isFunction(key)) {
            callback = key;
            key = [];
        }

        this.callbacks.push({
            key,
            callback,
        });
    };

    instance.onReceiveTimer = function (...rest: any) {
        this.warn('方法 onReceiveTimer 已废弃，请使用 subscribeTimer 代替');

        this.subscribeTimer(...rest);
    };

    instance.subscribeTimer = function (callback: any) {
        this.timerCallbacks.push(callback);
    };

    instance.subscribeOnline = function (callback: any) {
        this.onlineCallbacks.push(callback);
    };

    instance.batteryOnReceive = function (...rest: any) {
        this.warn('方法 batteryOnReceive 已废弃，请使用 subscribeBattery 代替');

        this.subscribeBattery(...rest);
    };

    instance.subscribeBattery = function (callback: any) {
        this.batteryCallbacks.push(callback);
    };

    instance.networkOnReceive = function (...rest: any) {
        this.warn('方法 networkOnReceive 已废弃，请使用 subscribeNetwork 代替');

        this.subscribeNetwork(...rest);
    };

    instance.subscribeNetwork = function (callback: any) {
        this.networkCallbacks.push(callback);
    };
};

export const prototypeCondition = (instance: any) => {
    instance.conditionCallbacks = [];

    instance.condition = function (condition: { key: string; value: any; method: string; params: any }, callback: any) {
        this.conditionCallbacks.push({
            condition,
            callback,
        });
    };
};

export const prototypeGroup = (instance: any) => {
    Object.defineProperties(instance, {
        /**
         * 长度
         */
        length: {
            get() {
                return this.to?.length || 0;
            },
        },
        /**
         * 迭代器
         */
        [Symbol.iterator]: {
            value() {
                this.i = -1;

                return {
                    next: () => {
                        let done = false;

                        if (++this.i >= this.length) {
                            done = true;
                            this.i = undefined;
                        }

                        return {
                            value: this,
                            done,
                        };
                    },
                };
            },
        },
    });
};
